﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX.DirectInput;
using System.Windows.Forms;

namespace MercuryFE
{
    public class Inputs
    {

        List<InputDevice> _inputDevices = null;

        //Control _formControl = null;
        IntPtr _handle = IntPtr.Zero;

        public Inputs(IntPtr winHandle, bool keyboard, bool mouse, bool joystick)
        {
            _inputDevices = new List<InputDevice>();
            _handle = winHandle;

            DirectInput di = new DirectInput();

            // keyboards
            if (keyboard)
            {
                foreach (DeviceInstance dev in di.GetDevices(DeviceClass.Keyboard, DeviceEnumerationFlags.AttachedOnly))
                {
                    InputDeviceType idt = InputDeviceType.keyboard;

                    InputDevice inputDevice = new InputDevice(winHandle, idt, dev.InstanceGuid, dev.ProductName);
                    _inputDevices.Add(inputDevice);
                }
            }

            // mice
            if (mouse)
            {
                foreach (DeviceInstance dev in di.GetDevices(DeviceClass.Pointer, DeviceEnumerationFlags.AttachedOnly))
                {
                    InputDeviceType idt = InputDeviceType.mouse;
                    InputDevice inputDevice = new InputDevice(winHandle, idt, dev.InstanceGuid, dev.ProductName);
                    _inputDevices.Add(inputDevice);
                }
            }

            // joysticks
            if (joystick)
            {
                foreach (DeviceInstance dev in di.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
                {
                    InputDeviceType idt = InputDeviceType.joystick;
                    InputDevice inputDevice = new InputDevice(winHandle, idt, dev.InstanceGuid, dev.ProductName);
                    _inputDevices.Add(inputDevice);
                }
            }


            // now initialise them all.
            foreach (InputDevice inputDevice in _inputDevices)
            {
                inputDevice.CreateDevice(di);
            }
        
        }



        public List<InputDeviceState> pollDevices() {
            //string buffer = "";

            List<InputDeviceState> states = new List<InputDeviceState>();

            foreach (InputDevice inputDevice in _inputDevices)
            {
                InputDeviceState ids = inputDevice.ReadData();

                if(ids != null) {
                    //string value = ids.ToString();

                    //if (value != null && value.Trim() != "")
                    //{
                    //    buffer += value;
                    //}
                    states.Add(ids);
                }

            }

            return states;

            //if (buffer != "")
            //    return buffer;
            //else
            //    return null;

        }

        public void releaseDevices()
        {
            foreach (InputDevice inputDevice in _inputDevices)
            {
                inputDevice.UnacquireDevice();
            }

        }

    }
}
